<?php
/*
 * Created on 18.04.2008
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 define ('MODEL_ITEM_FIND', 				'SELECT * FROM `content`,`menu` WHERE `content`.`id`=`menu`.`id` AND `menu`.`id`= ?');
 define ('MODEL_ITEM_FIND_BY_ALIAS',		'SELECT * FROM `menu` WHERE `alias` = ?');
 
 define ('MODEL_ITEM_SET_VISIBILITY',		'UPDATE `menu` SET `visibility` = ? WHERE `id` = ? ');
 
 define ('MODEL_ITEM_SAVE_BASIC_MENU',		'UPDATE `menu` SET `value` = ?, `alias` = ?, `icon` = ? WHERE `id` = ? ');
 define ('MODEL_ITEM_SAVE_BASIC_CONTENT',	'UPDATE `content` SET `text` = ? WHERE `id` = ? ');
 
 define ('MODEL_ITEM_SAVE_ADVANCED',		'UPDATE `content` SET `meta_desc` = ?, meta_keys = ? WHERE `id` = ?');
   
 define ('MODEL_ITEM_ADD_MENU',				'INSERT INTO `menu`  (`id` ,`value` ,`alias`) VALUES (NULL, ?, ? )');
 define ('MODEL_ITEM_ADD_CONTENT',			'INSERT INTO `content` (`id` ,`text` ,`date`) VALUES (?, ?, now())');
 define ('MODEL_ITEM_ADD_RELATION',			'INSERT INTO `menu_relations` ( `id` , `order_id` , `parent_id` ) VALUES ( ? , ((SELECT max(id) FROM menu ) +1), ?)');
 
define('MODEL_ITEM_DELETE_CONTENT',			'DELETE FROM `content` WHERE `id` = ?');
define('MODEL_ITEM_DELETE_MENU',			'DELETE FROM `menu` WHERE `id` = ?');
define('MODEL_ITEM_DELETE_MENU_RELATIONS',	'DELETE FROM `menu_relations` WHERE `id` = ?');
 
 class Model_Item extends Models_Base {
 	
 	public $item;
 	
 	function __construct($id) {
		parent::__construct();
 		$this->item = $this->find(array(id => $id));
 	}
 	
 	
 	function find($args) {
 		
 		if(isset($args[alias])) {
 			return $this->getRow(MODEL_ITEM_FIND_BY_ALIAS,array($args[alias]));
 		} else if(isset($args[id]))
 			return $this->getRow(MODEL_ITEM_FIND,array($args[id]));
 	}
 	
 	function get() {
 		return $this->item;
 	} 
 	
 	function save($args) {
 		
 		if(isset($args['visibility'])) {
 			$this->saveRow(MODEL_ITEM_SET_VISIBILITY,array( $args['visibility'], $this->item->id));
 			$this->item->visibility = $args['visibility'];
 		} else if(isset($args['basic'])) {
 			$this->saveRow(MODEL_ITEM_SAVE_BASIC_MENU,array( $args['value'], $args['alias'], $args['icon'], $this->item->id));
 			$this->saveRow(MODEL_ITEM_SAVE_BASIC_CONTENT,array( $args['text'],  $this->item->id));
 			
 			$this->item->text 	= $args['text'];
 			$this->item->value 	= $args['value'];
 			$this->item->alias 	= $args['alias'];
 			$this->item->icon	= $args['icon'];
 		} else if(isset($args['advanced'])) {
 			$this->saveRow(MODEL_ITEM_SAVE_ADVANCED,array( $args['meta_desc'], $args['meta_keys'],  $this->item->id));
 			$this->item->meta_desc = $args['meta_desc'];
 			$this->item->meta_keys = $args['meta_keys'];
 		}
 		
 	}
 	
 	function add($name, $alias) {
 		
 		if($this->find(array(alias => $alias)))
 			throw new ControllerException('exists_alias');

		$id = $this->insertRow(MODEL_ITEM_ADD_MENU,array($name,$alias));
		
		if(empty($id))
			throw new ControllerException('can\'t create menu item');
		else
			error_log("new id: $id");
		
		
		$this->insertRow(MODEL_ITEM_ADD_RELATION, array($id, 1));
		
		$this->insertRow(MODEL_ITEM_ADD_CONTENT, array($id,''));
		
		return new Model_Item($id);
		
 	}
 	
 	function delete() {
 		$this->execute(MODEL_ITEM_DELETE_CONTENT,array($this->get()->id));
 		$this->execute(MODEL_ITEM_DELETE_MENU,array($this->get()->id));
 		$this->execute(MODEL_ITEM_DELETE_MENU_RELATIONS,array($this->get()->id));
 	}
 	
 }
?>
